package gov.ccswj.modules.api.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.data.auditing.config.IsNewAwareAuditingHandlerBeanDefinitionParser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import gov.ccswj.common.utils.R;
import gov.ccswj.modules.api.annotation.AuthIgnore;
import gov.ccswj.modules.ccbusiness.entity.CBranchinfoEntity;
import gov.ccswj.modules.ccbusiness.entity.CChangeinfoEntity;
import gov.ccswj.modules.ccbusiness.entity.CHolderEntity;
import gov.ccswj.modules.ccbusiness.entity.CHonorEntity;
import gov.ccswj.modules.ccbusiness.entity.CInvestmentEntity;
import gov.ccswj.modules.ccbusiness.entity.CPatentinfoEntity;
import gov.ccswj.modules.ccbusiness.entity.CQualificationinfoEntity;
import gov.ccswj.modules.ccbusiness.entity.CRegisterinfoEntity;
import gov.ccswj.modules.ccbusiness.entity.CSoftwarecopyrightEntity;
import gov.ccswj.modules.ccbusiness.entity.CTrademarkinfoEntity;
import gov.ccswj.modules.ccbusiness.integratedata.IntegrateDate;
import gov.ccswj.modules.job.utils.JsonUtils;

/**
 * @author KY.Cheng
 * 大屏接口
 */
@RestController
@RequestMapping("/api/screen")
public class ApiScreenController {

	/**
	 * 大屏接口-基本信息
	 */
	@AuthIgnore
	@GetMapping("/register")
	public R info(String name, HttpServletResponse response) {
		// 允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");

		if (StringUtils.isBlank(name)) {
			return R.error("请输入公司名称");
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		map.put("registerInfo", registerInfo(name));
		List<Object> changeInfoList = changeInfo(name);
		map.put("changeInfo", changeInfoList);
		List<Object> holderInfoList = holderInfo(name);
		map.put("holderInfo", holderInfoList);
		
		
		return R.ok().put("data", map);
	}
	
	/**
	 * 大屏接口-资质荣誉
	 */
	@AuthIgnore
	@GetMapping("/honor")
	public R honor(String name, HttpServletResponse response) {
		// 允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");

		if (StringUtils.isBlank(name)) {
			return R.error("请输入公司名称");
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		map.put("honorInfo", honorInfo(name));
		map.put("qualifyCertifyInfo", qualifyCertifyInfo(name));
		
		return R.ok().put("data", map);
	}

	/**
	 * 大屏接口-知识产权
	 */
	@AuthIgnore
	@GetMapping("/intellectualproperty")
	public R intellectualproperty(String name, HttpServletResponse response) {
		// 允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");

		if (StringUtils.isBlank(name)) {
			return R.error("请输入公司名称");
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		map.put("patentInfo", patentInfo(name));
		map.put("trademarkInfo", trademarkInfo(name));
		map.put("softwarecopyrightInfo", softwarecopyrightInfo(name));
		
		return R.ok().put("data", map);
	}

	/**
	 * 大屏接口-企业投资
	 */
	@AuthIgnore
	@GetMapping("/investment")
	public R investment(String name, HttpServletResponse response) {
		// 允许跨域
		response.setHeader("Access-Control-Allow-Origin", "*");

		if (StringUtils.isBlank(name)) {
			return R.error("请输入公司名称");
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		map.put("investmentInfo", investmentInfo(name));
		map.put("branchinfoInfo", branchinfoInfo(name));
		
		return R.ok().put("data", map);
	}

	public Object registerInfo(String obj) {
		String resultdata = IntegrateDate.getCompanyData("getRegisterInfo", obj, 0);
		List<Object> list = JsonUtils.getdate(resultdata, CRegisterinfoEntity.class);
		if (null==list||list.size()==0) {
			return null;
		}
		return list.get(0);
	}

	public List<Object> changeInfo(String obj) {
		String resultdata = IntegrateDate.getCompanyData("getRegisterChangeInfo", obj, 0);
		List<Object> list = JsonUtils.getdate(resultdata, CChangeinfoEntity.class);
		return list;
	}

	public List<Object> holderInfo(String obj) {
		String resultdata = IntegrateDate.getCompanyData("getShareHolderInfo", obj, 0);
		List<Object> list = JsonUtils.getdate(resultdata, CHolderEntity.class);
		//出资时间倒序
		Collections.sort(list, new Comparator<Object>() {  
            @Override  
            public int compare(Object o1, Object o2) {  
            	CHolderEntity b1 =(CHolderEntity) o1;
            	CHolderEntity b2 =(CHolderEntity) o2;
            	Double i1 = 0.0;
            	Double i2 = 0.0;
            	if (StringUtils.isNotBlank(b1.getConratio())) {
            		i1 = Double.parseDouble(b1.getConratio());
				}
            	if (StringUtils.isNotBlank(b1.getConratio())) {
            		i2 = Double.parseDouble(b2.getConratio());
				}
            	Double i = i1 - i2;  
                if (i>0) {
					return -1;  
				}else if (i<0) {
					return 1;  
				}else{
					return 0;  
				}
            }  
        });
		return list;
	}

	public Object honorInfo(String obj) {
		String resultdata = IntegrateDate.getCompanyData("getEntHonorInfo", obj,1);
        List<Object> list=JsonUtils.getdate(resultdata,CHonorEntity.class);
		return list;
	}
	public Object qualifyCertifyInfo(String obj) {
		String resultdata = IntegrateDate.getCompanyData("getQualifyCertifyInfo", obj,1);
        List<Object> list=JsonUtils.getdate(resultdata,CQualificationinfoEntity.class);
		return list;
	}
	
	public Object patentInfo(String obj) {
        String resultdata = IntegrateDate.getCompanyData("getPatentInfo", obj,1);
        List<Object> list=JsonUtils.getdate(resultdata,CPatentinfoEntity.class);
		return list;
	}
	public Object trademarkInfo(String obj) {
        String resultdata = IntegrateDate.getCompanyData("getTrademarkInfo", obj,1);
        List<Object> list=JsonUtils.getdate(resultdata,CTrademarkinfoEntity.class);
        List<Object> list2=new ArrayList<Object>();
        for(Object c:list){
            CTrademarkinfoEntity cm= new CTrademarkinfoEntity();
            cm=(CTrademarkinfoEntity)c;
            cm.setEntname(obj);
            //查询专利详情
            String result = IntegrateDate.getCompanyData("getTrademarkDetailInfo",cm.getRowkey(),2);
            List<Object> resultlist=JsonUtils.getdate(result,CTrademarkinfoEntity.class);
            CTrademarkinfoEntity resultcm= new CTrademarkinfoEntity();
            resultcm=(CTrademarkinfoEntity)resultlist.get(0);
            //合并详情信息
            cm=(CTrademarkinfoEntity)IntegrateDate.combineSydwCore(cm, resultcm);
            list2.add(cm);
        }
        
		return list2;
	}
	public Object softwarecopyrightInfo(String obj) {
        String resultdata = IntegrateDate.getCompanyData("getSoftwareCopyrightInfo", obj,1);
        List<Object> list=JsonUtils.getdate(resultdata,CSoftwarecopyrightEntity.class);
		return list;
	}
	
	public Object investmentInfo(String obj) {
        String resultdata = IntegrateDate.getCompanyData("getInvestmentAbroadInfo", obj,0);
        List<Object> list=JsonUtils.getdate(resultdata,CInvestmentEntity.class);
		return list;
	}
	public Object branchinfoInfo(String obj) {
		String resultdata = IntegrateDate.getCompanyData("getBranchInfo", obj, 0);
		List<Object> list = JsonUtils.getdate(resultdata, CBranchinfoEntity.class);
		return list;
	}
	
}
